package com.mallify.admin.controller;

import com.mallify.common.model.user.UserTag;
import com.mallify.common.page.CommonPage;
import com.mallify.common.request.PageParamRequest;
import com.mallify.common.request.UserTagRequest;
import com.mallify.common.response.Result;
import com.mallify.service.service.UserTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户标签 前端控制器
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/user/tag")
@Api(tags = "会员 -- 标签")
public class UserTagController {

    @Resource
    private UserTagService userTagService;

    /**
     * 分页显示用户分标签
     *
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:user:tag:list')")
    @ApiOperation(value = "分页显示用户分标签")
    @GetMapping(value = "/list")
    public Result<CommonPage<UserTag>> list(@Validated PageParamRequest pageParamRequest) {
        CommonPage<UserTag> userTagCommonPage = CommonPage.restPage(userTagService.getList(pageParamRequest));
        return Result.success(userTagCommonPage);
    }

    /**
     * 新增用户分标签
     *
     * @param userTagRequest 新增参数
     */
    @PreAuthorize("hasAuthority('admin:user:tag:save')")
    @ApiOperation(value = "新增用户分标签")
    @PostMapping(value = "/save")
    public Result<String> save(@RequestBody @Validated UserTagRequest userTagRequest) {
        if (Boolean.TRUE.equals(userTagService.create(userTagRequest))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 删除用户分标签
     *
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:user:tag:delete')")
    @ApiOperation(value = "删除用户分标签")
    @GetMapping(value = "/delete")
    public Result<String> delete(@RequestParam(value = "id") Integer id) {
        if (userTagService.delete(id)) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 修改用户标签
     *
     * @param id             integer id
     * @param userTagRequest 修改参数
     */
    @PreAuthorize("hasAuthority('admin:user:tag:update')")
    @ApiOperation(value = "修改用户标签")
    @PostMapping(value = "/update")
    public Result<String> update(@RequestParam(value = "id") Integer id, @RequestBody @Validated UserTagRequest userTagRequest) {
        if(Boolean.TRUE.equals(userTagService.updateTag(id, userTagRequest))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 查询用户标签
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:user:tag:info')")
    @ApiOperation(value = "查询用户标签")
    @GetMapping(value = "/info")
    public Result<UserTag> info(@RequestParam(value = "id") Integer id) {
        return Result.success(userTagService.getById(id));
    }

}